package com.cloud.phone.task;


import android.os.Looper;

import com.cloud.phone.tools.MyUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 使用方式
 * TaskStepBean
 *          .addStep(StepNeedDoing)
 *          .addStep(StepNeedDoing)
 *          .addStep(StepNeedDoing)
 *          .start(listener)
 */
public class TaskStepBean {
    public static  boolean isTaskDoing=false;
    private static TaskStepBean taskStepBean;
    private static List<StepNeedDoing> stepNeedDoings;
    private long stepDelayTime=2000;//每一步操作之间的时间间隔

    public TaskStepBean addStep(StepNeedDoing stepNeedDoing){
        TaskStepBean.stepNeedDoings.add(stepNeedDoing);
        return taskStepBean;
    }



    public void start(OnTaskOverListener onTaskOverListener) throws InterruptedException {
        if(isTaskDoing){
            if(onTaskOverListener!=null){
                onTaskOverListener.onTaskFail("已有任务正在执行");
            }
            return;
        }
        if(MyUtils.isEmpty(stepNeedDoings)){
            if(onTaskOverListener!=null){
                onTaskOverListener.onTaskFail("请先添加需要执行的步骤");
            }
            return;
        }
        isTaskDoing=true;
        if(Looper.getMainLooper()==Looper.myLooper()){
            new Thread(){
                @Override
                public void run() {
                    try {
                        String err = null;
                        for (StepNeedDoing stepListener : stepNeedDoings) {
                            String errMsg = stepListener.onStep();
                            if (!MyUtils.isEmpty(errMsg)) {
                                err = errMsg;
                                break;
                            }
                            Thread.sleep(stepDelayTime);
                        }
                        if(MyUtils.isEmpty(err)){
                            //任务全部完成
                            if(onTaskOverListener!=null){
                                onTaskOverListener.onTaskSuccess();
                            }
                        }else{
                            //任务中间有部分未完成，存在错误信息
                            if(onTaskOverListener!=null){
                                onTaskOverListener.onTaskFail(err);
                            }
                        }
                        stepNeedDoings.clear();
                        isTaskDoing=false;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        stepNeedDoings.clear();
                        isTaskDoing=false;
                    }
                }
            }.start();
        }else{
            try {
                String err = null;
                for (StepNeedDoing stepListener : stepNeedDoings) {
                    String errMsg = stepListener.onStep();
                    if (!MyUtils.isEmpty(errMsg)) {
                        err = errMsg;
                        break;
                    }
                    Thread.sleep(stepDelayTime);
                }
                if(MyUtils.isEmpty(err)){
                    //任务全部完成
                    if(onTaskOverListener!=null){
                        onTaskOverListener.onTaskSuccess();
                    }
                }else{
                    //任务中间有部分未完成，存在错误信息
                    if(onTaskOverListener!=null){
                        onTaskOverListener.onTaskFail(err);
                    }
                }
                stepNeedDoings.clear();
                isTaskDoing=false;
            } catch (InterruptedException e) {
                e.printStackTrace();
                stepNeedDoings.clear();
                isTaskDoing=false;
                if(onTaskOverListener!=null){
                    onTaskOverListener.onTaskFail("线程异常");
                }
            }
        }

    }

    public interface StepNeedDoing {
        String onStep() throws InterruptedException;
    }


    public interface OnTaskOverListener{
        void onTaskSuccess() throws InterruptedException;
        void onTaskFail(String err) throws InterruptedException;
    }

    public static TaskStepBean getInstance() {
        if(taskStepBean==null){
            taskStepBean=new TaskStepBean();
            stepNeedDoings=new ArrayList<>();
        }
        return taskStepBean;
    }
}
